package com.seagox.chat.chat.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.chat.chat.entity.Depart;
import com.seagox.chat.chat.entity.DepartUser;
import com.seagox.chat.chat.mapper.DepartMapper;
import com.seagox.chat.chat.mapper.DepartUserMapper;
import com.seagox.chat.chat.service.IDepartService;
import com.seagox.chat.common.ResultCode;
import com.seagox.chat.common.ResultData;
import com.seagox.chat.util.TreeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DepartService implements IDepartService {
    
    @Autowired
    private DepartMapper departMapper;
    
    @Autowired
    private DepartUserMapper departUserMapper;

	@Override
	public ResultData queryAllData() {
		LambdaQueryWrapper<Depart> qw = new LambdaQueryWrapper<>();
		qw.eq(Depart::getStatus, 1).orderByAsc(Depart::getCode);
		List<Depart> list = departMapper.selectList(qw);
		return ResultData.success(TreeUtils.listToTreeByRule(JSONArray.parseArray(JSON.toJSONString(list)), "code", "33333333"));
	}
	
	@Override
	public ResultData queryAllUserData(String userId, String disabled) {
		LambdaQueryWrapper<Depart> qw = new LambdaQueryWrapper<>();
		qw.eq(Depart::getStatus, 1).orderByAsc(Depart::getCode);
		List<Depart> list = departMapper.selectList(qw);
		List<Map<String,Object>> userData = departMapper.queryAllUserData();
		JSONObject userObject = new JSONObject();
		List<String> disabledList = new ArrayList<>();
		if(!StringUtils.isEmpty(disabled)) {
			disabledList = Arrays.asList(disabled.split(","));
		}
		for(int i=0;i<userData.size();i++) {
			Map<String,Object> user = userData.get(i);
			if(userId.equals(user.get("id").toString())) {
				user.put("disabled", true);
			}
			if(disabledList.contains(user.get("id").toString())) {
				user.put("disabled", true);
			}
			String code = user.get("code").toString();
			JSONArray userSet = new JSONArray();
			if(userObject.containsKey(code)) {
				userSet = userObject.getJSONArray(code);
			}
			userSet.add(user);
			userObject.put(code, userSet);
		}
		return ResultData.success(TreeUtils.listToTreeByRule(JSONArray.parseArray(JSON.toJSONString(list)), "code", "33333333", userObject));
	}

	@Override
	public ResultData insert(Depart depart) {
		LambdaQueryWrapper<Depart> qw = new LambdaQueryWrapper<>();
		qw.eq(Depart::getCode, depart.getCode()).eq(Depart::getStatus, 1);
		long count = departMapper.selectCount(qw);
		if(count == 0) {
			departMapper.insert(depart);
			return ResultData.success(null);
		} else {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "部门编码存在");
		}
	}

	@Override
	public ResultData edit(Depart depart) {
		Depart original = departMapper.selectById(depart.getId());
		if(!depart.getCode().equals(original.getCode())) {
			LambdaQueryWrapper<Depart> qw = new LambdaQueryWrapper<>();
			qw.eq(Depart::getCode, depart.getCode()).eq(Depart::getStatus, 1);
			long count = departMapper.selectCount(qw);
			if(count == 0) {
				departMapper.updateById(depart);
				return ResultData.success(null);
			} else {
				return ResultData.warn(ResultCode.PARAMETER_ERROR, "部门编码存在");
			}
		} else {
			departMapper.updateById(depart);
			return ResultData.success(null);
		}
	}

	@Override
	public ResultData delete(String id) {
		LambdaQueryWrapper<DepartUser> qw = new LambdaQueryWrapper<>();
		qw.eq(DepartUser::getDepartId, id);
		long count = departUserMapper.selectCount(qw);
		if(count == 0) {
			Depart depart = new Depart();
			depart.setId(id);
			depart.setStatus(2);
			departMapper.updateById(depart);
			return ResultData.success(null);
		} else {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "部门下有用户，不可删除");
		}
	}

}
